package mark.tom.friend.roll.test;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.TextView;

import mark.tom.friend.ConfigContent;
import mark.tom.friend.FRoll;

public class MainActivity extends AppCompatActivity {

    private TextView appPackageNameTextView;
    private TextView appDebugAbleTextView, appServerConfigTextView, appExtraConfigTextView;
    private AlertDialog mDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件
        appPackageNameTextView = findViewById(R.id.appPackageNameTextView);
        appDebugAbleTextView = findViewById(R.id.appDebugAbleTextView);
        appServerConfigTextView = findViewById(R.id.appServerConfigTextView);
        appExtraConfigTextView = findViewById(R.id.appExtraConfigTextView);
        // 设置当前的应用包名
        appPackageNameTextView.setText(getPackageName());
        // 设置文字的上下滚动
        appServerConfigTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
        appExtraConfigTextView.setMovementMethod(ScrollingMovementMethod.getInstance());
        // 设置刷新按钮点击事件
        findViewById(R.id.contentRefreshBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 刷新内容
                refreshContent();
            }
        });
        // 设置切换包名按钮点击事件
        findViewById(R.id.packageNameChangeBtn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == mDialog) {
                    mDialog = createDialog();
                }
                if (!mDialog.isShowing()) {
                    mDialog.show();
                }
            }
        });
        // 刷新数据并呈现在界面上
        refreshContent();
    }

    @NonNull private AlertDialog createDialog() {
        return new AlertDialog
                .Builder(this, R.style.Theme_AppCompat_Light_Dialog_Alert)
                .setTitle(R.string.dialog_title)
                .setView(R.layout.dialog_package_switch)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        TextView packageNameEditText = mDialog.findViewById(R.id.packageNameEditText);
                        if (null == packageNameEditText) return;
                        String packageName = packageNameEditText.getText().toString();
                        if (TextUtils.isEmpty(packageName)) {
                            packageName = getPackageName();
                        }
                        appPackageNameTextView.setText(packageName);
                        // 刷新内容
                        refreshContent();
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .create();
    }

    private void refreshContent() {
        ConfigContent appSettingBean = FRoll.get(appPackageNameTextView.getText().toString());
        appDebugAbleTextView.setText(String.valueOf(appSettingBean.isDebugAble()));
        appServerConfigTextView.setText(appSettingBean.getServerConfig());
        appExtraConfigTextView.setText(appSettingBean.getExtraConfig());
    }

}
